/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Beavers;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import util.RMSCounter;

/**
 * @author Ferry
 */
public class Zakat extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    private RMSCounter conter;
    int oldCont = 0;
  
    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private mainCanvas mainCanvas;
    private TextBox textBox;
    private Alert alert;
    private Command Edit;
    private Command okCommand;
    private Command exitCommand;
    private Command okCommand1;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The Zakat constructor.
     */
    public Zakat() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getMainCanvas());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: mainCanvas ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of mainCanvas component.
     * @return the initialized component instance
     */
    public mainCanvas getMainCanvas() {
        if (mainCanvas == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            mainCanvas = new mainCanvas();//GEN-BEGIN:|14-getter|1|14-postInit
            mainCanvas.setTitle("mainCanvas");//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
            mainCanvas.setMidlet(this);
        }//GEN-BEGIN:|14-getter|2|
        return mainCanvas;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    public void masukTextbox(int posisi) {
        switchDisplayable(null, getTextBox());
        if (posisi == 0) {
            textBox.setTitle("Total Income");
            if (mainCanvas.TotalIncome == 0) {
                textBox.setString(null);
            } else {
                textBox.setString(Integer.toString(mainCanvas.TotalIncome));
            }
        } else if (posisi == 1) {
            textBox.setTitle("Total Outcome");
            if (mainCanvas.TotalOutcome == 0) {
                textBox.setString(null);
            } else {
                textBox.setString(Integer.toString(mainCanvas.TotalOutcome));

            }
        }
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textBox ">//GEN-BEGIN:|16-getter|0|16-preInit
    /**
     * Returns an initiliazed instance of textBox component.
     * @return the initialized component instance
     */
    public TextBox getTextBox() {
        if (textBox == null) {//GEN-END:|16-getter|0|16-preInit
            // write pre-init user code here
            textBox = new TextBox("textBox", null, 100, TextField.NUMERIC);//GEN-BEGIN:|16-getter|1|16-postInit
            textBox.addCommand(getOkCommand());
            textBox.setCommandListener(this);//GEN-END:|16-getter|1|16-postInit
            // write post-init user code here
        }//GEN-BEGIN:|16-getter|2|
        return textBox;
    }
    //</editor-fold>//GEN-END:|16-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Edit ">//GEN-BEGIN:|17-getter|0|17-preInit
    /**
     * Returns an initiliazed instance of Edit component.
     * @return the initialized component instance
     */
    public Command getEdit() {
        if (Edit == null) {//GEN-END:|17-getter|0|17-preInit
            // write pre-init user code here
            Edit = new Command("Ok", Command.OK, 0);//GEN-LINE:|17-getter|1|17-postInit
            // write post-init user code here
        }//GEN-BEGIN:|17-getter|2|
        return Edit;
    }
    //</editor-fold>//GEN-END:|17-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == alert) {//GEN-BEGIN:|7-commandAction|1|40-preAction
            if (command == exitCommand) {//GEN-END:|7-commandAction|1|40-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|2|40-postAction
                // write post-action user code here
            } else if (command == okCommand1) {//GEN-LINE:|7-commandAction|3|38-preAction
                // write pre-action user code here
                switchDisplayable(null, getMainCanvas());//GEN-LINE:|7-commandAction|4|38-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|21-preAction
        } else if (displayable == textBox) {
            if (command == okCommand) {//GEN-END:|7-commandAction|5|21-preAction
                // write pre-action user code here

                if (mainCanvas.posisi == 0) {
                    if (textBox.getString().equals(null) || textBox.getString().equals("")) {
//                        setIn(mainCanvas.TotalIncome = 0);
                         mainCanvas.TotalIncome = 0;
                    } else {
//                        setIn(mainCanvas.TotalIncome = Integer.parseInt(textBox.getString()));
                         mainCanvas.TotalIncome = Integer.parseInt(textBox.getString());
                    }

                    switchDisplayable(null, getMainCanvas());
                } else if (mainCanvas.posisi == 1) {
                    if (textBox.getString().equals(null) || textBox.getString().equals("")) {
//                        setOut(mainCanvas.TotalOutcome = 0);
                        mainCanvas.TotalOutcome = 0;
                    } else {
                        mainCanvas.TotalOutcome = Integer.parseInt(textBox.getString());
//                        setOut(mainCanvas.TotalOutcome = Integer.parseInt(textBox.getString()));
                    }
//                    int hasil = getIn() - getOut();
                    int ttl = mainCanvas.TotalIncome - mainCanvas.TotalOutcome;
                    mainCanvas.setHasil(ttl);
                    System.out.println("in= " + mainCanvas.TotalIncome + " out= " + mainCanvas.TotalOutcome + " hasil=" + ttl);


                    conter = new RMSCounter();
                    conter.open();
                    if (conter.isEmpty()) {
                        oldCont = 1;
                        conter.writeRecord(String.valueOf(oldCont));
                    } else if (!conter.isEmpty()) {
                        oldCont = conter.readRecords(1);
                        oldCont += 1;
                        conter.updateRecord(1, String.valueOf(oldCont));
                        System.out.println("counter =" + oldCont);

                    }
                    conter.close();

                    if (oldCont <= 5) {
                        switchDisplayable(null, getMainCanvas());
                    } else {
                        mainCanvas.setHasil(0);
                        switchDisplayable(null, getAlert());
                    }
//GEN-LINE:|7-commandAction|6|21-postAction
                }

                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|7-postCommandAction
        }//GEN-END:|7-commandAction|7|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|8|
    //</editor-fold>//GEN-END:|7-commandAction|8|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|20-getter|0|20-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|20-getter|0|20-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|20-getter|1|20-postInit
            // write post-init user code here

        }//GEN-BEGIN:|20-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|20-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert ">//GEN-BEGIN:|35-getter|0|35-preInit
    /**
     * Returns an initiliazed instance of alert component.
     * @return the initialized component instance
     */
    public Alert getAlert() {
        if (alert == null) {//GEN-END:|35-getter|0|35-preInit
            // write pre-init user code here
            alert = new Alert("INFO", "\"This is trial Version, can only be used 5 times. Please buy the full version\"", null, AlertType.INFO);//GEN-BEGIN:|35-getter|1|35-postInit
            alert.addCommand(getOkCommand1());
            alert.addCommand(getExitCommand());
            alert.setCommandListener(this);
            alert.setTimeout(Alert.FOREVER);//GEN-END:|35-getter|1|35-postInit
            // write post-init user code here
        }//GEN-BEGIN:|35-getter|2|
        return alert;
    }
    //</editor-fold>//GEN-END:|35-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of okCommand1 component.
     * @return the initialized component instance
     */
    public Command getOkCommand1() {
        if (okCommand1 == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            okCommand1 = new Command("Ok", Command.OK, 0);//GEN-LINE:|37-getter|1|37-postInit
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return okCommand1;
    }
    //</editor-fold>//GEN-END:|37-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|39-getter|1|39-postInit
            // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|39-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {


        if (midletPaused) {
            resumeMIDlet();
        } else {

            initialize();

            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
        this.notifyDestroyed();
    }

    
}
